<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Ae_Article extends Controller {

	public function action_index()
	{
		Include_Ae_Login::check_login();
		$Marticle = Model::factory('Article');
		$view = View::factory('ae/article');
		$err_msg = '';
		
		$param = $this->request->param('stuff');
		if (!empty($param)){
			$param_arr = explode('/', $param);
		}
		$page = isset($param_arr[0]) ? $param_arr[0] : 1;
		
		$list = $Marticle->get_list_page_admin($page, 20);
		$view->bind('list', $list['result']);
		$view->bind('page_html', $list['page']);
		
		// 文章类型
		$category = Kohana::$config->load('category')->as_array();
		$view->bind('category', $category);
		
		$view->bind('err_msg', $err_msg);
		
		$head_arr = array(
			'page_title' => '文章列表',
			'controller' => 'article',
			'action' => 'index'
		);
		$view->bind('head_arr', $head_arr);
		$this->response->body($view);
	}
	
	public function action_compile()
	{
		Include_Ae_Login::check_login();
		$Marticle = Model::factory('Article');
		$view = View::factory('ae/article');
		$err_msg = '';
		
		$param = $this->request->param('stuff');
		if (!empty($param)){
			$param_arr = explode('/', $param);
		}
		$opt = isset($param_arr[0]) ? $param_arr[0] : 'publish'; // edit
		$aid = isset($param_arr[1]) ? $param_arr[1] : -1;
		
		// 发布/修改
		if ($this->request->method() == 'POST'){
			$subdata = $this->request->post();
			if (isset($opt)){
				// 检查提交数据
				$re_check = $Marticle->check_param($subdata);
				if ($re_check === true){
					// tag handle
					$tag_arr = array();
					if (!empty($subdata['tags'])){
						$tag_arr = explode(',', trim($subdata['tags']));
						foreach ($tag_arr as $key => &$tag){
							$tag = trim($tag);
							if (empty($tag)){
								unset($tag_arr[$key]);
							}
						}
						$subdata['tags'] = implode(',', $tag_arr);
					}
					
					if ($opt == 'publish'){
						// 发布
						$id_add = $Marticle->add($subdata);
						if ($id_add === false){
							$err_msg = '写数据库失败';
						}else {
							// tag handling
							if (!empty($tag_arr)){
								$Mtag = Model::factory('Tag');
								$tagids = $Mtag->get_tagid($tag_arr);
								$exists_tagid = $not_exists_tag = array();
								foreach ($tagids as $_tagid){
									if ($_tagid['id'] == -1){
										$not_exists_tag[] = $_tagid['tag'];
									}else {
										$exists_tagid[] = $_tagid['id'];
									}
								}
								$new_tagids = $Mtag->add($not_exists_tag);
								$_exists_tagid = array_merge($exists_tagid, $new_tagids);
								$Mtag->add_map($id_add, $_exists_tagid);
								$Mtag->up_count($exists_tagid);
							}
							
							header('Location: /ae/article');
							exit;
						}
					}else if($opt == 'edit'){
						// 修改
						unset($subdata['aid']);
						$subdata['uptime'] = date('Y-m-d H:i:s');
						$subdata['status'] = 2;
						$re = $Marticle->update($subdata, $aid);
						if ($re == false){
							$err_msg = '更新数据库失败';
						}else {
							// tag handling
							$Mtag = Model::factory('Tag');
							if (!empty($tag_arr)){
								$tagids = $Mtag->get_tagid($tag_arr);
								$exists_tagid = $not_exists_tag = array();
								foreach ($tagids as $_tagid){
									if ($_tagid['id'] == -1){
										$not_exists_tag[] = $_tagid['tag'];
									}else {
										$exists_tagid[] = $_tagid['id'];
									}
								}
								$new_tagids = $Mtag->add($not_exists_tag);
								$_exists_tagid = array_merge($exists_tagid, $new_tagids);
								
								$map_exists_tagid = $Mtag->get_tagid_by_aid($aid);
								$tagids_add = array_diff($_exists_tagid, $map_exists_tagid);
								$Mtag->add_map($aid, $tagids_add);
								$tagids_count_add = array_diff($exists_tagid, $map_exists_tagid);
								$Mtag->up_count($tagids_count_add);
								$tagids_del = array_diff($map_exists_tagid, $_exists_tagid);
								$Mtag->del_map($aid, $tagids_del);
								$Mtag->up_count($tagids_del, 0);
							}else {
								$map_exists_tagid = $Mtag->get_tagid_by_aid($aid);
								$Mtag->del_map($aid, $map_exists_tagid);
								$Mtag->up_count($map_exists_tagid, 0);
							}
							
							header('Location: /ae/article');
							exit;
						}
					}else {
						$err_msg = '地址参数错误';
					}
				}else {
					$err_msg = $re_check;
				}
			}else {
				$err_msg = '地址参数错误';
			}
			$view->bind('article_cont', $subdata);
		}
		
		// 文章类型
		$category = Kohana::$config->load('category')->as_array();
		$view->bind('category', $category);
		
		if ($opt == 'edit'){
			$article_cont = $Marticle->get_by_aid($aid);
			$view->bind('article_cont', $article_cont);
		}
		
		$view->bind('opt', $opt);
		$view->bind('err_msg', $err_msg);
		
		$head_arr = array(
			'page_title' => '文章编辑',
			'controller' => 'article',
			'action' => 'index'
		);
		$view->bind('head_arr', $head_arr);
		$this->response->body($view);
	}
	
	public function action_operate()
	{
		Include_Ae_Login::check_login();
		$Marticle = Model::factory('Article');
		$err_msg = '';
		
		$param = $this->request->param('stuff');
		if (!empty($param)){
			$param_arr = explode('/', $param);
		}
		$opt = isset($param_arr[0]) ? $param_arr[0] : 'del'; // del shield preview release
		$aid = isset($param_arr[1]) ? $param_arr[1] : -1;
		
		if ($this->request->method() == 'POST'){
			$subdata = $this->request->post();
			if (is_array($subdata['aids']) && !empty($subdata['aids'])){
				if ($subdata['opt'] == 'del'){
					$re = $Marticle->del($subdata['aids']);
				}elseif($subdata['opt'] == 'shield') {
					$re = $Marticle->up_status(1, $subdata['aids']);
				}elseif ($subdata['opt'] == 'release'){
					$re = $Marticle->up_status(0, $subdata['aids']);
				}
				if (isset($re) && $re){
					header('Location: /ae/article');
					exit;
				}else {
					$err_msg = '操作失败';
				}
			}else {
				$err_msg = '没有选择文章';
			}
		}elseif ($opt == 'del' && $aid > -1){
			$re = $Marticle->del($aid);
			if ($re == false){
				$err_msg = '删除失败';
			}else {
				header('Location: /ae/article');
				exit;
			}
		}elseif ($opt == 'release' && $aid > -1){
			$re = $Marticle->up_status(0, $aid);
			if ($re == false){
				$err_msg = '发布失败';
			}else {
				header('Location: /ae/article');
				exit;
			}
		}
		exit($err_msg);
	}

}
